package br.com.edumobi.adapters;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import br.com.edumobi.R;
import br.com.edumobi.dados.modelos.Questao;

public class ListaQuestoesAdapter extends BaseAdapter {

    private List<Questao> questoes;
    private boolean verificarRespostaQuestao;

    public ListaQuestoesAdapter(List<Questao> questoes) {
        this.questoes = questoes;
    }

    public ListaQuestoesAdapter(List<Questao> questoes, boolean verificarRespostaQuestao) {
        this.questoes = questoes;
        this.verificarRespostaQuestao = verificarRespostaQuestao;
    }

    @Override
    public int getCount() {
        return questoes.size();
    }

    @Override
    public Object getItem(int position) {
        return questoes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return questoes.get(position).id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Questao questao = questoes.get(position);

        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lista_questoes, null);

            holder = new ViewHolder();

            holder.textViewItemListaQuestoesNumero = (TextView) convertView.findViewById(R.id.textViewItemListaQuestoesNumero);
            holder.textViewItemListaQuesoesEnunciado = (TextView) convertView.findViewById(R.id.textViewItemListaQuesoesEnunciado);
            holder.textViewItemListaQuestoesNivel = (TextView) convertView.findViewById(R.id.textViewItemListaQuestoesNivel);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textViewItemListaQuestoesNumero.setText(String.valueOf(position + 1));
        holder.textViewItemListaQuesoesEnunciado.setText(questao.enunciado);
        holder.textViewItemListaQuestoesNivel.setText(questao.nivel.nivel);

        if (questao.respondida && verificarRespostaQuestao) {
            if (questao.acertou) {
                holder.textViewItemListaQuestoesNumero.setTextColor(Color.parseColor("#ff0a8cbf"));
                holder.textViewItemListaQuesoesEnunciado.setTextColor(Color.parseColor("#ff0a8cbf"));
            } else {
                holder.textViewItemListaQuestoesNumero.setTextColor(Color.parseColor("#fff02100"));
                holder.textViewItemListaQuesoesEnunciado.setTextColor(Color.parseColor("#fff02100"));
            }
        } else {
            holder.textViewItemListaQuestoesNumero.setTextColor(Color.parseColor("#ff232323"));
            holder.textViewItemListaQuesoesEnunciado.setTextColor(Color.parseColor("#ff232323"));
        }

        return convertView;
    }

    private class ViewHolder {
        TextView textViewItemListaQuestoesNumero;
        TextView textViewItemListaQuesoesEnunciado;
        TextView textViewItemListaQuestoesNivel;
    }
}